//
//  ViewController.m
//  BALPractice
//
//  Created by zhuhao on 15/3/5.
//  Copyright (c) 2015年 zhuhao. All rights reserved.
//

#import "ViewController.h"
#import "BALLoginService.h"

@interface ViewController ()

@property(nonatomic,strong)UIButton *endButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [BALLoginService loginWithUserName:@"710126898@qq.com" password:@"qawsedrf"];
    
    _endButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 500, 100, 30)];
    _endButton.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_endButton];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cm_center_discount"]];
    imageView.contentMode=UIViewContentModeScaleToFill;
    imageView.frame=CGRectMake(0, 0, 20, 20);
    
    CALayer *layer = [CALayer layer];
    layer.contents = imageView.layer.contents;
    layer.opacity = 1;
    layer.frame = imageView.frame;
    [self.view.layer addSublayer:layer];
    
    CGPoint startPoint = [[touches anyObject] locationInView:self.view];
    CGPoint endpoint = _endButton.center;
    
    imageView.center = startPoint;
    
    //贝塞尔曲线中间点
    float sx=startPoint.x;
    float sy=startPoint.y;
    float ex=endpoint.x;
    float ey=endpoint.y;
    float x=sx+(ex-sx)/3;
    float y=sy+(ey-sy)*0.5-400;
    CGPoint centerPoint=CGPointMake(x,y);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    [path addQuadCurveToPoint:endpoint controlPoint:centerPoint];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.duration = 1;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.autoreverses = NO;
    animation.delegate = self;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    [layer addAnimation:animation forKey:@"bu"];
 
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    [UIView animateWithDuration:0.3 animations:^{
        _endButton.transform = CGAffineTransformScale(_endButton.transform, 1.2, 1.2);
    } completion:^(BOOL finished) {
        _endButton.transform = CGAffineTransformIdentity;
    }];
    int addPrice = 20;
    int allPrice = 0;
    allPrice = allPrice + addPrice;
    NSString *str = [NSString stringWithFormat:@"￥%i",allPrice];
    [_endButton setTitle:str forState:UIControlStateNormal];
    
//    //加入购物车动画效果
//    UILabel *addLabel = [[UILabel alloc ]init];
//    addLabel.backgroundColor = [UIColor blueColor];
//    addLabel.frame = CGRectMake(_endButton.frame.origin.x+100, _endButton.frame.origin.y, _endButton.frame.size.width, _endButton.frame.size.height);
//    [addLabel setText:[NSString stringWithFormat:@"+%i",addPrice]];
//    [self.view addSubview:addLabel];
//    
//    CALayer *transitionLayer1 = [[CALayer alloc] init];
//    [CATransaction begin];
//    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
//    transitionLayer1.opacity = 1.0;
//    transitionLayer1.contents = (id)addLabel.layer.contents;
//    transitionLayer1.frame = [[UIApplication sharedApplication].keyWindow convertRect:addLabel.bounds fromView:addLabel];
//    [[UIApplication sharedApplication].keyWindow.layer addSublayer:transitionLayer1];
//    [CATransaction commit];
//    
//    CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
//    positionAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(addLabel.frame.origin.x+30, addLabel.frame.origin.y+20)];
//    positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(addLabel.frame.origin.x+30, addLabel.frame.origin.y)];
//    
//    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
//    opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
//    opacityAnimation.toValue = [NSNumber numberWithFloat:0];
//    
//    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
//    rotateAnimation.fromValue = [NSNumber numberWithFloat:0 * M_PI];
//    rotateAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
//    
//    CAAnimationGroup *group = [CAAnimationGroup animation];
//    group.beginTime = CACurrentMediaTime();
//    group.duration = 0.3;
//    group.animations = [NSArray arrayWithObjects:positionAnimation,opacityAnimation,nil];
//    group.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
////    group.delegate = self;
//    group.fillMode = kCAFillModeForwards;
//    group.removedOnCompletion = NO;
//    group.autoreverses= NO;
//    [transitionLayer1 addAnimation:group forKey:@"opacity"];
}

@end
